package com.delivery.www.servlet;

import com.alibaba.fastjson.JSON;
import com.delivery.www.po.Order;
import com.delivery.www.po.User;
import com.delivery.www.service.OrderService;
import com.delivery.www.constant.Constants;
import com.delivery.www.service.ServiceImpl.OrderServiceImpl;
import com.delivery.www.service.ServiceImpl.UserServiceImpl;
import com.delivery.www.service.UserService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * @author 12970
 */
public class OrderServlet extends BaseServlet {
    OrderService orderService = new OrderServiceImpl();
    UserService userService = new UserServiceImpl();

    /**
     * 购物车下单
     *
     * @param req  请求
     * @param resp 响应
     * @throws IOException 异常
     */
    public void cartOrder(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        PrintWriter writer = resp.getWriter();
        User user = (User) req.getSession().getAttribute(Constants.USER_SESSION);
        OrderServiceImpl orderService = new OrderServiceImpl();
        String dishId = req.getParameter("dishId");
        List<Integer> dishIds = JSON.parseArray(dishId, Integer.class);
        Float money = orderService.judgeBalance(dishIds);
        if (user.getBalance() < money) {
            writer.print("balance");
        } else {
            boolean b = orderService.cartOrder(user.getUserId(), dishIds);
            if (b) {
                User userById = userService.getUserById(user.getUserId());
                req.getSession().setAttribute(Constants.USER_SESSION,userById);
                writer.print("ok");
            } else {
                writer.print("error");
            }
        }
    }


    /**
     * 优惠券下单
     *
     * @param req  请求
     * @param resp 响应
     * @throws IOException 异常
     */
    public void couponOrder(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        User user = (User) req.getSession().getAttribute(Constants.USER_SESSION);
        PrintWriter writer = resp.getWriter();
        int storeId = Integer.parseInt(req.getParameter("storeId"));
        Float money = Float.parseFloat(req.getParameter("money"));
        int dishId = Integer.parseInt(req.getParameter("dishId"));
        int couponId = Integer.parseInt(req.getParameter("couponId"));
        if (user.getBalance() < money) {
            writer.print("balance");
        } else {
            boolean b = orderService.addOrder(user.getUserId(), storeId, dishId, money, couponId);
            if (b) {
                User userById = userService.getUserById(user.getUserId());
                req.getSession().setAttribute(Constants.USER_SESSION,userById);
                writer.print("ok");
            } else {
                writer.print("error");
            }
        }
    }

    /**
     * 下单
     *
     * @param req  请求
     * @param resp 响应
     * @throws IOException 异常
     */
    public void orderOne(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        User user = (User) req.getSession().getAttribute(Constants.USER_SESSION);
        PrintWriter writer = resp.getWriter();
        if (user == null) {
            writer.print("login");
        } else {
            int dishId = Integer.parseInt(req.getParameter("dishId"));
            int storeId = Integer.parseInt(req.getParameter("storeId"));
            Float amount = Float.valueOf(req.getParameter("amount"));
            if (user.getBalance() < amount) {
                writer.print("balance");
            } else {
                boolean b = orderService.addOrder(user.getUserId(), storeId, dishId, amount, 0);
                if (b) {
                    User userById = userService.getUserById(user.getUserId());
                    req.getSession().setAttribute(Constants.USER_SESSION,userById);
                    writer.print("ok");
                } else {
                    writer.print("error");
                }
            }
        }
    }

    /**
     * 获取订单
     *
     * @param req  请求
     * @param resp 响应
     * @throws IOException 异常
     */
    public void selectOrder(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        PrintWriter writer = resp.getWriter();
        int storeId = Integer.parseInt(req.getParameter("storeId"));
        List<Order> orders = orderService.getCompleteOrder(storeId, 2);
        writer.print(JSON.toJSONString(orders));
    }

    /**
     * 更改订单
     *
     * @param req  请求
     * @param resp 响应
     * @throws IOException 异常
     */
    public void modifyOrder(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        User user = (User) req.getSession().getAttribute(Constants.USER_SESSION);
        PrintWriter writer = resp.getWriter();
        int orderId = Integer.parseInt(req.getParameter("orderId"));
        int storeId = Integer.parseInt(req.getParameter("storeId"));
        boolean b;
        if (user.getIdentity() == Constants.TWO) {
            b = orderService.modifyStatus(orderId, 1, storeId);
        } else {
            b = orderService.modifyStatus(orderId, 2, storeId);
        }
        if (b) {
            writer.print("ok");
        } else {
            writer.print("error");
        }
    }


    /**
     * 用户订单
     *
     * @param req  请求
     * @param resp 响应
     * @throws IOException 异常
     */
    public void userOrder(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        User user = (User) req.getSession().getAttribute(Constants.USER_SESSION);
        PrintWriter writer = resp.getWriter();
        List<Order> orders = orderService.getOwnOrder(user.getUserId());
        writer.print(JSON.toJSONString(orders));
    }

    /**
     * 获取未完成订单
     *
     * @param req  请求
     * @param resp 响应
     * @throws IOException 异常
     */
    public void selectUnOrder(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        PrintWriter writer = resp.getWriter();
        int storeId = Integer.parseInt(req.getParameter("storeId"));
        List<Order> unCompleteOrder = orderService.getUnCompleteOrder(storeId, 2);
        writer.print(JSON.toJSONString(unCompleteOrder));
    }

}